package com.example.customview.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import kotlin.math.cos
import kotlin.math.sin

private val RADIUS = 150f.dp
private val ANGLES = floatArrayOf(60f, 100f, 120f, 80f)
private val COLORS = listOf(
        Color.parseColor("#2879FD"), Color.parseColor("#C1175C"),
        Color.parseColor("#009688"), Color.parseColor("#FF8E00"),
)
private val OFFSET_LENGTH = 50f

class PieView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var chooseIndex = 2

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
    }

    override fun onDraw(canvas: Canvas) {
        var startAngle = 0f
        for ((index, angle) in ANGLES.withIndex()) {
            paint.color = COLORS[index]
            if (index == chooseIndex) {
                canvas.save()
                canvas.translate(OFFSET_LENGTH* cos(Math.toRadians((startAngle + angle/2f).toDouble())).toFloat(),
                        OFFSET_LENGTH* sin(Math.toRadians((startAngle + angle/2f).toDouble())).toFloat())
            }
            canvas.drawArc(width / 2f - RADIUS, height / 2f - RADIUS, width / 2f + RADIUS, height / 2f + RADIUS,
                    startAngle, angle, true, paint)
            if (index == chooseIndex) {
                canvas.restore()
            }
            startAngle += angle
        }
    }
}